package com.lovo.project.servlet.permission;

import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PermissionDO;
import com.lovo.project.service.PermissionService;
import com.lovo.project.service.impl.PermissionServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 修改权限
 * @author: luo
 * @date: 2020/11/5 22:36
 */
@WebServlet(name = "UpdatePermissionServlet",urlPatterns = "/permission/updatePermission")
public class UpdatePermissionServlet extends HttpServlet {


    private PermissionService permissionService = new PermissionServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String permissionName = request.getParameter("permissionName");
        String permissionDescribe = request.getParameter("permissionType");
        String url = request.getParameter("permissionUrl");
        Integer id = Integer.parseInt(request.getParameter("permissionId"));
        PermissionDO permissionDO = new PermissionDO();
        permissionDO.setPermissionId(id);
        permissionDO.setPermissionName(permissionName);
        permissionDO.setPermissionType(permissionDescribe);
        permissionDO.setPermissionUrl(url);
        PrintWriter out = response.getWriter();
        try {
            permissionService.updatePermission(permissionDO);
            out.print(ResultVO.success().toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(UserCodeEnum.USER_UNKNOWN_ERROR).toJson());
        }finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }



}
